import storage from "@/utils/storage.js";
import {
	logout
} from "@/api/login";
import Vue from "vue";
/**
 * 判断用户是否登录
 * @param val  如果为auth则判断是否登录
 * 如果传入 auth 则为判断是否登录
 */
export function isLogin(val) {
	let userInfo = storage.getUserInfo();
	if (val == "auth") {
		return userInfo && userInfo.id ? true : false;
	} else {
		return storage.getUserInfo();
	}
}
export function tipsToLogin(type) {
	if (!isLogin("auth")) {
		uni.showModal({
			title: "提示",
			content: "当前用户未登录是否登录？",
			confirmText: "确定",
			cancelText: "取消",
			confirmColor: Vue.prototype.$mainColor,
			success: (res) => {
				if (res.confirm) {
					navigateToLogin();
				} else if (res.cancel) {
					if (type !== 'normal') {
						uni.navigateBack();
					}

				}
			},
		});
		return false;
	}
	return true;
}

/**
 * 跳转到登录页面
 */
export function navigateToLogin(type = "navigateTo") {
	/**
	 * 此处进行条件编译判断
	 * 微信小程序跳转到微信小程序登录页面
	 * H5/App跳转到普通登录页面
	 */
	uni[type]({
		url: "/pages/passport/login",
	});
}

/**
 * 退出登录
 *
 */
export function quiteLoginOut() {
	uni.showModal({
		title: "提示",
		content: "是否退出登录？",
		confirmColor: Vue.prototype.$mainColor,
		async success(res) {
			if (res.confirm) {
				storage.setAccessToken("");
				storage.setRefreshToken("");
				storage.setUserInfo({});
				storage.setHasLogin(false)
				navigateToLogin("redirectTo");
				await logout();
			}
		},
	});
}



/**
 * 订单状态列表
 */
export function orderStatusList(val) {
	let orderStatusList = {
		UNDELIVERED: "待发货",
		UNPAID: "未付款",
		PAID: "已付款",
		PARTS_DELIVERED: "部分发货",
		DELIVERED: "已发货",
		CANCELLED: "已取消",
		COMPLETED: "已完成",
		COMPLETE: "已完成",
		TAKE: "待核验",
		STAY_PICKED_UP: "待自提",
	};
	return orderStatusList[val];
}